#include "Random.h"
#include <time.h>
#include <iostream>

namespace grng      // avoid namespace pollution
{
    // don't set directly, use grng_setEngine to set (called automatically by grng_make_vg when necessary)
    static boost::shared_ptr<grng_engine> _rng_generator;

    void grng_setEngine(boost::shared_ptr<grng_engine> ptr)
    {
        if(!ptr)
            throw std::runtime_error("Tried to set generator to NULL pointer");
        if(_rng_generator)
            throw std::runtime_error("Tried to set generator more than once");

        _rng_generator = ptr;
    }

    grng_engine& grng_getEngine()
    {
        if(_rng_generator)
            return *_rng_generator;

        // if doesn't exist, set, seeding with current system time
        grng_setEngine(boost::shared_ptr<grng_engine>(new grng_engine(unsigned int(time(NULL)))));

        if(_rng_generator)
            return *_rng_generator;

        // if still doesn't exist, error
        throw std::runtime_error("Requested RNG, but there isn't one");
    }
}
//
//// make a global instance of CRandom01 for use with RandLim & RandIdx
//CRandom01 grng::_C01;